/*
 * copyright (c) 2013 Blaise-Florentin Collin
 *
 * This file is part of astroDCAM.
 *
 * AstroAviBrowser is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License v3
 * as published by the Free Software Foundation
 *
 * AstroAviBrowser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * v3 along with AstroAviBrowser; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 */

///
/// \file QAstroApplication.cpp
///
/// \brief Main Qt application object (code)
///


#ifdef WINDOWS_TARGET

// C includes
#include <stdio.h>

// C++ includes
#include "windows.h"

#endif

// Qt includes
#include <QMessageBox>

// C++ app includes
#include "config.hpp"
#include "dc1394bus.hpp"

// Qt app includes
#include "QAstroApplication.hpp"

//
// Constructor
//
QAstroApplication::QAstroApplication(int argc,char** argv) : QApplication(argc,argv) {
   ieee1394bus=NULL;
   mainWindow=NULL;

   // parsing arguments
   args=arguments();
   for(int i=0;i<args.size();i++) {
      // checking for the verbose flag
      if(args.at(i)==QString("--verbose")) {
         verbose=true;
         #ifdef WINDOWS_TARGET
         // we have to create a console
         AllocConsole();
         // redirects cout to the console
         freopen("CONOUT$","w",stdout);
         #endif
      } else
         verbose=false;
   }

   // gretting
   TRACE(APP_NAME << " " << BINARY_VERSION << " build " << BUILD_VERSION)
   TRACE(APP_COPYRIGHT)
}

//
// Destructor
//
QAstroApplication::~QAstroApplication() {
   if(mainWindow)
      delete mainWindow;
   if(ieee1394bus)
      delete ieee1394bus;
}

//
// Init the application
//
int QAstroApplication::init() {
   // init the ieee1394 bus
   ieee1394bus=new dc1394bus();
   if((ieee1394bus==NULL)||!ieee1394bus->isReady()) {
      QMessageBox::critical(0,"astroDCAM",tr("Unable init ieee1394 bus, leaving..."));
      return(_NO_IEEE1394_);
   }
   if(ieee1394bus->getCameraNumber()<1) {
      QMessageBox::critical(0,"astroDCAM",tr("No camera found, leaving..."));
      return(_NO_CAMERA_);
   }

   // main window
   mainWindow=new QAstroMainWindow(ieee1394bus,this);
   mainWindow->buildGUI();
   mainWindow->show();
   return(0);
}
